/**
 * Copyright &copy; 2012-2016 <a href="https://github.com/thinkgem/jeesite">JeeSite</a> All rights reserved.
 */
package com.thinkgem.jeesite.modules.stock.web;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.thinkgem.jeesite.modules.stock.entity.*;
import com.thinkgem.jeesite.modules.stock.service.PdClService;
import com.thinkgem.jeesite.modules.stock.service.WarehouseService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.thinkgem.jeesite.common.config.Global;
import com.thinkgem.jeesite.common.persistence.Page;
import com.thinkgem.jeesite.common.web.BaseController;
import com.thinkgem.jeesite.common.utils.StringUtils;
import com.thinkgem.jeesite.modules.stock.service.PdService;

import java.util.List;

/**
 * 盘点Controller
 * @author 盘点
 * @version 2019-12-21
 */
@Controller
@RequestMapping(value = "${adminPath}/stock/pd")
public class PdController extends BaseController {

	@Autowired
	private PdService pdService;
	
	@ModelAttribute
	public Pd get(@RequestParam(required=false) String id) {
		Pd entity = null;
		if (StringUtils.isNotBlank(id)){
			entity = pdService.get(id);
		}
		if (entity == null){
			entity = new Pd();
		}
		return entity;
	}
	
	@RequiresPermissions("stock:pd:view")
	@RequestMapping(value = {"list", ""})
	public String list(Pd pd, HttpServletRequest request, HttpServletResponse response, Model model) {
		Page<Pd> page = pdService.findPage(new Page<Pd>(request, response), pd); 
		model.addAttribute("page", page);
		return "modules/stock/pdList";
	}
	@Autowired
	private WarehouseService warehouseService;
	@Autowired
	private PdClService pdClService;


	@RequiresPermissions("stock:pd:view")
	@RequestMapping(value = "form")
	public String form(Pd pd, Model model) {

		model.addAttribute("pd", pd);
		if (org.apache.commons.lang3.StringUtils.isNotBlank(pd.getId())){
			// 环节编号
			String taskDefKey = pd.getAct().getTaskDefKey();

			if(taskDefKey.equals("audit1")){
				return "modules/stock/pdaudit1Form";
			}
			else if(taskDefKey.equals("audit2")){

				return "modules/stock/pdaudit2Form";
			}
			else if(taskDefKey.equals("audit3")){

				return "modules/stock/pdaudit3Form";
			}
			else if(taskDefKey.equals("audit4")){

				return "modules/stock/pdaudit4Form";
			}

		}
		else{
			model.addAttribute("cks", warehouseService.findList(new Warehouse()));
			model.addAttribute("pdcls", pdClService.findList(new PdCl()));
		}

		return "modules/stock/pdForm";
	}
	/**
	 * 工单执行（完成任务）
	 * @param model
	 * @return
	 */
	@RequiresPermissions("stock:pd:edit")
	@RequestMapping(value = "saveAudit")
	public String saveAudit(Pd pd, Model model) {
		if (org.apache.commons.lang3.StringUtils.isBlank(pd.getAct().getFlag())
				|| org.apache.commons.lang3.StringUtils.isBlank(pd.getAct().getComment())){
			addMessage(model, "请填写审核意见。");
			return form(pd, model);
		}
		pdService.auditSave(pd);
		return "redirect:" + adminPath + "/act/task/todo/";
	}

	@RequiresPermissions("stock:pd:view")
	@RequestMapping(value = "view")
	public String view(Pd pd, Model model) {

		model.addAttribute("pd", pd);
		return "modules/stock/pdView";
	}
	@RequiresPermissions("stock:pd:edit")
	@RequestMapping(value = "save")
	public String save(Pd pd, Model model, RedirectAttributes redirectAttributes) {
		if (!beanValidator(model, pd)){
			return form(pd, model);
		}
		pdService.save(pd);
		addMessage(redirectAttributes, "保存盘点成功");
		return "redirect:" + adminPath + "/act/task/todo/";
	}
	
	@RequiresPermissions("stock:pd:edit")
	@RequestMapping(value = "delete")
	public String delete(Pd pd, RedirectAttributes redirectAttributes) {
		pdService.delete(pd);
		addMessage(redirectAttributes, "删除盘点成功");
		return "redirect:"+Global.getAdminPath()+"/stock/pd/?repage";
	}

}